import tkinter as tk
import math

class TrigonometryCalculator(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("Trigonometry Calculator")
        self.geometry("300x200")

        self.input_var = tk.StringVar()
        self.result_var = tk.StringVar(value="Result will appear here")

        # Create and place the input entry
        self.input_entry = tk.Entry(self, textvariable=self.input_var, width=20)
        self.input_entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

        # Create and place the function buttons
        self.sin_button = tk.Button(self, text="sin", command=self.calculate_sin)
        self.sin_button.grid(row=1, column=0, padx=10, pady=10)

        self.cos_button = tk.Button(self, text="cos", command=self.calculate_cos)
        self.cos_button.grid(row=1, column=1, padx=10, pady=10)

        self.tan_button = tk.Button(self, text="tan", command=self.calculate_tan)
        self.tan_button.grid(row=1, column=2, padx=10, pady=10)

        # Create and place the result label
        self.result_label = tk.Label(self, textvariable=self.result_var, width=30, anchor='w')
        self.result_label.grid(row=2, column=0, columnspan=3, padx=10, pady=10)

    def calculate_sin(self):
        try:
            angle = float(self.input_var.get())
            result = math.sin(math.radians(angle))
            self.result_var.set(f"sin({angle}) = {result:.4f}")
        except ValueError:
            self.result_var.set("Please enter a valid number")

    def calculate_cos(self):
        try:
            angle = float(self.input_var.get())
            result = math.cos(math.radians(angle))
            self.result_var.set(f"cos({angle}) = {result:.4f}")
        except ValueError:
            self.result_var.set("Please enter a valid number")

    def calculate_tan(self):
        try:
            angle = float(self.input_var.get())
            # Check for angles where tan is undefined (90 degrees + multiples of 180)
            if angle % 180 == 90:
                result = "undefined"
            else:
                result = math.tan(math.radians(angle))
                # Limit the number of decimal places for readability
                if isinstance(result, float):
                    result = f"{result:.4f}"
            self.result_var.set(f"tan({angle}) = {result}")
        except ValueError:
            self.result_var.set("Please enter a valid number")

if __name__ == "__main__":
    app = TrigonometryCalculator()
    app.mainloop()